#include <iostream>
#include <fstream>
#include <stdexcept>
#include <cmath> 
#include <vector>
#include "Function.hpp"
#include "Interpolation.hpp"


double polynomial(double x, int n) {
    // 数据点
    std::vector<double> days = {0, 6, 10, 13, 17, 20, 28};
    std::vector<double> weightsSp1 = {6.67, 17.3, 42.7, 37.3, 30.1, 29.3, 28.7};
    std::vector<double> weightsSp2 = {6.67, 16.1, 18.9, 15.0, 10.6, 9.44, 8.89};
    // 创建牛顿插值实例
    NewtonInterpolation newtonSp1(days, weightsSp1);
    NewtonInterpolation newtonSp2(days, weightsSp2);
        if(n==1){
            return newtonSp1.interpolate(x);
        }
        else if(n==2){
            return newtonSp2.interpolate(x);
        }   
        else{
            std::cerr << "False input" << std::endl;
        }
        return 0;
    }
int main() {
    //绘图
    for (int n = 1; n <= 2; n += 1) {
        std::ofstream file("data_E" + std::to_string(n) + ".csv");
        if (!file.is_open()) {
            std::cerr << "Failed to open file" << std::endl;
            return 1;
        }

        for (double x = 0; x <= 30; x += 0.1) {
            double y = polynomial(x, n);
            file << x << "," << y << std::endl;
        }

        file.close();
    }

    // 预测接下来的15天
    double daysToPredict = 15;
    double predictedWeightSp1 = polynomial(28 + daysToPredict,1);
    double predictedWeightSp2 = polynomial(28 + daysToPredict,2);

    std::cout << "Predicted weight for Sp1 after " << daysToPredict << " days: " << predictedWeightSp1 << std::endl;
    std::cout << "Predicted weight for Sp2 after " << daysToPredict << " days: " << predictedWeightSp2 << std::endl;

    // 判断是否会死亡
    if (predictedWeightSp1 <= 0 || predictedWeightSp2 <= 0) {
        std::cout << "Both samples of larvae will die after another " << daysToPredict << " days." << std::endl;
    } else {
        std::cout << "Both samples of larvae will not die after another " << daysToPredict << " days." << std::endl;
    }

    return 0;
}